package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import lombok.*;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.code = ServiceCode.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static JsonResult<Void> fail(ServiceCode code, String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code.getValue();
        jsonResult.message = message;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

}
